﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Citrix.XaAdminMobile.ViewModel;

namespace Citrix.XaAdminMobile.ViewCommon
{
    /// <summary>
    /// Interaction logic for SessionsView.xaml
    /// </summary>
    public partial class SessionsView : UserControl
    {
        /// <summary>
        /// A reference to the associated v-m object.
        /// </summary>
        private SessionsViewModel vm = null;

        /// <summary>
        /// Ctor
        /// </summary>
        public SessionsView()
        {
            InitializeComponent();
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(SessionsView_DataContextChanged);
        }

        /// <summary>
        /// Tracks data context changes to hold a ref to the current v-m.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SessionsView_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (null != this.vm)
            {
                this.vm.SetSearchFocus -= ViewModel_SetSearchFocus;
            }

            this.vm = e.NewValue as SessionsViewModel;
            if (null != this.vm)
            {
                this.vm.SetSearchFocus += ViewModel_SetSearchFocus;
            }
        }

        /// <summary>
        /// Set focus to the search input text box when the v-m requests it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewModel_SetSearchFocus(object sender, EventArgs e)
        {
            if (!this.searchInput.IsKeyboardFocusWithin)
            {
                this.searchInput.Focus();
            }
        }

        /// <summary>
        /// Used to grab input focus into the search input text box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchInputTextBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((e.NewValue is bool) && (bool)e.NewValue && !this.searchInput.IsKeyboardFocusWithin)
            {
                this.searchInput.Focus();
            }
        }

        /// <summary>
        /// Key up handler for search input text box. We look for enter key presses so we can
        /// kick off a new search and to hide the onscreen keyboard.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Key up event args.</param>
        private void SearchInput_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter && this.vm != null)
            {
                // The search input text box shows/hides the keyboard when it gains/loses focus.
                // So to make the keyboard hide, we set focus to the active list.
                if (this.SessionList.Visibility == System.Windows.Visibility.Visible)
                {
                    this.SessionList.Focus();
                }
                else if (this.SearchResults.Visibility == System.Windows.Visibility.Visible)
                {
                    this.SearchResults.Focus();
                }

                // Tell the v-m to start a new search.
                this.vm.StartSearch();
            }
        }

        /// <summary>
        /// The search option check handler sets focus back to the search input text so the user
        /// can immediately enter input without having to manually set focus back.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            this.searchInput.Focus();
        }
    }
}
